%% Load Data and add subfolders to path 

clear; close all; clc; 

load ../data/music_dataset.mat
addpath 'CV/' 'DT/' 'Lyrics_Kernel/' 'SVM/libsvm/'

[Xt_lyrics] = make_lyrics_sparse(train, vocab);
[Xq_lyrics] = make_lyrics_sparse(quiz, vocab);

Yt = zeros(numel(train), 1);
for i=1:numel(train)
    Yt(i) = genre_class(train(i).genre);
end

Xt_audio = make_audio(train);
Xq_audio = make_audio(quiz);

%% Stem Data

Xt_lyrics = stemmer(Xt_lyrics,vocab);       % Stem train set
Xq_lyrics = stemmer(Xq_lyrics,vocab);       % Stem quiz set
fprintf('Stemming Complete \n');

%% Optimal value from CV
opt_c = 1;          % for scaled data

%% Train SVM model
load('scale_factors.mat');
Xt_lyrics = (Xt_lyrics - repmat(scale_min,size(Xt_lyrics,1),1))* ...
    spdiags(1./(scale_max - scale_min)',0,size(Xt_lyrics,2),size(Xt_lyrics,2));

kernel = kernel_intersection(Xt_lyrics,Xt_lyrics);

svm_lyrics = svmtrain(Yt, [(1:size(kernel,1))' kernel],...
    sprintf('-t 4 -b 1 -c %g', opt_c));

model.train_data = Xt_lyrics; 
model.svm = svm_lyrics; 
model.vocab = vocab; 
save('my_model.mat', 'model'); 


%% Make prediction on single example 

% Make kernelized example to train on
tic; 

for i = 1:5
    model = init_model(vocab); 
    ranks = make_final_prediction(model, train(1)); 
    %fprintf('Class Prediction: %d \n', ranks(1)); 
end 

toc 